﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Data;
using System.ComponentModel;
using Microsoft.Practices.Prism.ViewModel;

namespace QuickSearchAndSelect
{
    public class SearchAndSelectViewModel : NotificationObject
    {
        private ICollectionView products;
        private Product selectedItem;
        private List<Product> source=new List<Product>();
        public List<Product> Source 
        { 
            get
            {
                return source;
            }
            set
            {
                this.products = CollectionViewSource.GetDefaultView(value);
                this.itemCount = value.Count;
                this.products.Filter = ContainsFilter;
            }
        }

        public SearchAndSelectViewModel()
        {
            
        }

        public ICollectionView Products
        {
            get
            {
                return this.products;
            }
        }

        public Product SelectedItem
        {
            get
            {
                return this.selectedItem;
            }
            set
            {
                if (this.selectedItem != value)
                {
                    this.selectedItem = value;
                }
            }
        }

        public int SelectedIndex
        {
            get { return Products.CurrentPosition; }
            set
            {
                if (value >= 0 && value <= itemCount)
                    if (Products.MoveCurrentToPosition(value))
                    {
                        SelectedItem = Products.CurrentItem as Product;
                        RaisePropertyChanged(() => SelectedIndex);
                    }
            }
        }

        private string filter = string.Empty;
        private int itemCount = 0;
        public string Filter
        {
            get
            {
                return this.filter;
            }
            set
            {
                if (this.filter != value)
                {
                    this.filter = value;
                    this.Products.Refresh();
                }
            }
        }

        private bool ContainsFilter(object item)
        {
            var product = item as Product;
            if (product == null)
            {
                return false;
            }
            if (string.IsNullOrEmpty(this.Filter))
            {
                return true;
            }
            if (product.Name.ToUpperInvariant().Contains(this.Filter.ToUpperInvariant()))
            {
                return true;
            }
            return false;
        }

        //private bool NameContainsA(object item)
        //{
        //    var product = item as Product;
        //    if (product == null)
        //    {
        //        return false;
        //    }
        //    return product.Name.ToUpperInvariant().Contains("A");
        //}
    }
}
